package com.corner.web.controller.mqtt;

import com.alibaba.fastjson2.JSON;
import com.corner.system.mqtt.MqttSendClient;
import com.corner.system.domain.MqttReq;
import com.corner.system.service.JxMqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description : MQTT测试类
 * @Author : Sherlock
 * @Date : 2023/8/1 16:35
 */
@RestController
@RequestMapping("/mqtt")
public class MqttController {

    @Autowired
    private MqttSendClient mqttSendClient;
    @Autowired
    private JxMqttService jxMqttService;

//    @GetMapping(value = "/publishTopic")
//    public String publishTopic(String topic, String sendMessage) {
//        System.out.println("topic:" + topic);
//        System.out.println("message:" + sendMessage);
//        this.mqttSendClient.publish(false, topic, sendMessage);
//        return "topic:" + topic + "\nmessage:" + sendMessage;
//    }

    @GetMapping(value = "/publishTopic")
    public String publishTopic1(Long bid) throws NoSuchFieldException, IllegalAccessException {
        MqttReq mqttReq = jxMqttService.selectWswDataList(bid);
        String mqttReqStr = JSON.toJSONString(mqttReq);
        this.mqttSendClient.publish(false, null, mqttReqStr);
        return "topic:data/up/2024/KN20240001202404/dataUpload\nmessage:aaaaaaaaaaaaaaa" ;
    }

    @GetMapping(value = "/selectWswDataList")
    public Map selectWswDataList(Long bid) throws NoSuchFieldException, IllegalAccessException {
        MqttReq mqttReq = jxMqttService.selectWswDataList(bid);
        return mqttReq.getData();
    }

}
